<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

/* const animationDuration = 6000*/

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    barData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      const dataname = []
      const dataset = []
      for (var i = 0; i < this.barData.data.length; i++) {
        var col = new echarts.graphic.LinearGradient(
          0, 0, 0, 1,
          [
            { offset: 0, color: '#0570e9' },
            { offset: 1, color: '#0ecff8' }
          ]
        )
        if ((i + 1) % 2 === 0) {
          col = new echarts.graphic.LinearGradient(
            0, 0, 0, 1,
            [
              { offset: 0, color: '#303e8c' },
              { offset: 1, color: '#932ae1' }
            ]
          )
        }
        dataname.push(this.barData.data[i].name)
        dataset.push({
          value: this.barData.data[i].value,
          itemStyle: {
            color: col,
            barBorderRadius: [50, 50, 50, 50]
          }
        })
      }
      this.chart.setOption(
        {
          title: {
            subtext: '渐变',
            x: 'left',
            subtextStyle: {
              color: '94e5ff',
              textAlign: 'left',
              fontSize: 16
            }
          },
          xAxis: {
            type: 'category',
            data: dataname,
            axisLine: { // 这是x轴文字颜色
              lineStyle: {
                color: '#94e5ff'
              }
            },
            axisLabel: {
              interval: 0,
              rotate: 40,
              inside: false,
              margin: 5,
              fontSize: 10
            },
            axisTick: { // 去刻度
              show: false
            },
            splitLine: {
              show: false
            }
          },
          yAxis: {
            type: 'value',
            axisLine: {
              lineStyle: {
                color: '#94e5ff'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              margin: 5,
              fontSize: 10
            },
            splitLine: { // 去除网格线
              show: false
            }
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线， 可选为：'line' | 'shadow'
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '5%',
            containLabel: true
          },
          series: [
            {
              data: dataset,
              type: 'bar',
              barWidth: 15
            }
          ]

        }
      )
    }
  }
}
</script>

